import java.util.Scanner;
public class Main{
    //一定要记得坐标是y ,x!!!
    public static void main(String[] args){
        int[][] arr = {
                {1, 1, 1, 1, 1},
                {1, 0, 1, 0, 0},
                {1, 1, 0, 1, 1},
                {0, 1, 1, 1, 1},
                {2, 1, 1, 0, 1}
        };
        for(int i = 0;i < arr.length;i++) {
            for(int j = 0;j < arr[0].length;j++){
                System.out.print(arr[i][j] + "  ");
            }
            System.out.println();
        }
        int [][] next = {
                {1, 0},
                {0, 1},
                {-1, 0},
                {0, -1}
        };
        int[][] book = new int[5][5];
        int head = 0;
        int tail = 0;
        int step = 0;
        int flag = 0;
        note[] p = new note[25];
        //初始化
        System.out.println("请输入初始坐标(realX,realY),注:(realX = x - 1,realY = y - 1)");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        set(p,tail,x,y,step);
        tail++;
        book[y][x] = 1;
        while(head < tail){
            for(int i = 0;i < next.length;i++){
                int tx = p[head].getX() + next[i][1];
                int ty = p[head].getY() + next[i][0];
                if(tx < 0 || tx > 4 || ty < 0 || ty > 4)
                    continue;
                if(arr[ty][tx] == 0 || book[ty][tx] == 1)
                    continue;
                book[ty][tx] = 1;
                set(p,tail,tx,ty,p[head].getStep() + 1);
                tail++;
                if(arr[ty][tx] == 2){
                    flag = 1;
                    break;
                }
            }
            if(flag == 1)
                break;
            head++;
        }
        System.out.println("从第"+(p[0].getY() + 1)+"行第"+(p[0].getX() + 1)+"列到2最少需要需要"+(p[head].getStep() + 1)+ "步");
    }
    public static void set(note[] p,int n,int x,int y,int step){
       note temp = new note(x,y,step);
       p[n] = temp;
    }
}